<?php
include_once('lib/common.php');


class vmw_VM extends vmw_Page
{
    protected $props, $hostProps;


    public function __construct($title = 'VM Info')
    {
        parent::__construct($title);
    }


    protected function GetJQBody()
    {
        //
        // Get the status of the virtual machine in a human readable
        // form.
        //
        if ($this->props['summary.runtime.powerState'] == 'poweredOn')
            $state = 'On';
        else if ($this->props['summary.runtime.powerState'] == 'poweredOff')
            $state = 'Off';
        else if ($this->props['summary.runtime.powerState'] == 'suspended')
            $state = 'Suspended';
        else
            $state = 'Unknown';

        $content =  '<div data-role="collapsible" data-collapsed="false" data-content-theme="d">' . "\r\n";
        $content .= '  <h3 style="margin: 0px;">Summary</h3>' . "\r\n";
        $content .= '  <ul data-role="listview" data-inset="true">' . "\r\n";
        $content .= $this->GetTitleValuePair('Name', $this->props['summary.config.name']);
        $content .= $this->GetTitleValuePair('Host', $this->hostProps['name']);
        $content .= $this->GetTitleValuePair('State', $state);
        $content .= '  </ul>';
        $content .= '</div>';

        $content .= '<div data-role="collapsible" data-collapsed="false" data-content-theme="d">' . "\r\n";
        $content .= '  <h3 style="margin: 0px;">Guest Info</h3>' . "\r\n";
        $content .= '  <ul data-role="listview" data-inset="true">' . "\r\n";
        $content .= $this->GetTitleValuePair('Host Name', $this->props['summary.guest.hostName']);
        $content .= $this->GetTitleValuePair('IP Address', $this->props['summary.guest.ipAddress']);
        $content .= $this->GetTitleValuePair('Memory', $this->props['summary.config.memorySizeMB'] . 'MB');
        $content .= $this->GetTitleValuePair('CPUs', $this->props['summary.config.numCpu']);
        $content .= $this->GetTitleValuePair('Guest OS', $this->props['summary.config.guestFullName']);
        $content .= $this->GetTitleValuePair('Annotation', $this->props['summary.config.annotation']);
        $content .= '  </ul>';
        $content .= '</div>';

        $content .= '<div data-role="collapsible" data-collapsed="false" data-content-theme="d">' . "\r\n";
        $content .= '  <h3 style="margin: 0px;">Runtime</h3>' . "\r\n";
        $content .= '  <ul data-role="listview" data-inset="true">' . "\r\n";
        $content .= $this->GetTitleValuePair('CPU Usage', $this->props['summary.quickStats.overallCpuUsage'] . 'MHz');
        $content .= $this->GetTitleValuePair('Guest Memory', $this->props['summary.quickStats.guestMemoryUsage'] . 'MB');
        $content .= $this->GetTitleValuePair('Host Memory', $this->props['summary.quickStats.hostMemoryUsage'] . 'MB');
        $content .= $this->GetTitleValuePair('Uptime', $this->SecondsToTime($this->props['summary.quickStats.uptimeSeconds']));
        $content .= '  </ul>';
        $content .= '</div>';

        return $content;
    }


    private function GetTitleValuePair($title, $value)
    {
        $content =  '    <li style="padding-top: 3px; padding-bottom: 3px;" data-theme="c">' . "\r\n";
        $content .= '      <p style="margin-top: 0px;"><strong>' . htmlspecialchars($title) . '</strong></p>' . "\r\n";
        $content .= '      <p style="margin-bottom: 0px;">' . ($value != '' ? htmlspecialchars($value) : '&nbsp;') . '</p>' . "\r\n";
        $content .= '    </li>' . "\r\n";

        return $content;
    }


    private function SecondsToTime($seconds)
    {
        $time = '';

        if ($seconds == '')
            return '';

        if ($seconds >= 86400)
        {
            $num = ($seconds / 86400);
            $seconds %= 86400;

            if ($num > 0)
                $time .= sprintf('%d days, ', $num);
        }

        if ($seconds >= 3600)
        {
            $num = ($seconds / 3600);
            $seconds %= 3600;

            if ($num > 0)
                $time .= sprintf('%d:', $num);
        }

        $min = ($seconds / 60);
        $seconds %= 60;
        $time .= sprintf('%02d:%02d', $min, $seconds);

        return trim($time);
    }


    public function ProcessRequest()
    {
        global $vmware;

        $p = array( 'summary.config.name',
		'summary.runtime.host',
		'summary.runtime.powerState',
		'summary.guest.hostName',
		'summary.guest.ipAddress',
		'summary.config.memorySizeMB',
		'summary.config.numCpu',
		'summary.config.guestFullName',
		'summary.config.annotation',
		'summary.quickStats.overallCpuUsage',
		'summary.quickStats.guestMemoryUsage',
		'summary.quickStats.hostMemoryUsage',
		'summary.quickStats.uptimeSeconds',
		'summary.overallStatus'
		);

        //
        // Get the information about the virtual machine and the host name.
        //
        $vm = vic_ManagedObjectReference::VirtualMachine($_REQUEST['vm']);
        $this->props = $vmware->GetObjectProperties($vm, $p);
        $host = vic_ManagedObjectReference::HostSystem($this->props['summary.runtime.host']);
        $this->hostProps = $vmware->GetObjectProperties($host, 'name');

        $this->SetPageTitle($this->props['summary.config.name']);
        $this->pageHeader = vic_Enum::ManagedEntityStatus($this->props['summary.overallStatus'], vic_Enum::AS_IMAGE, array('class' => 'headerIcon')) .
            ' ' . htmlspecialchars($this->props['summary.config.name']);

        parent::ProcessRequest();
    }
}

vmware_session();
$page = new vmw_VM();
$page->ProcessRequest();
?>
